package com.example.baserecyclerviewadapterdemo.activity;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.baserecyclerviewadapterdemo.R;
import com.example.baserecyclerviewadapterdemo.adapter.EmptyViewAdapter;
import com.example.baserecyclerviewadapterdemo.data.DataServer;

public class EmptyViewUseActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private EmptyViewAdapter mAdapter;
    private boolean error = true;
    private boolean noData = true;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_empty_view_use);

        final Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mAdapter = new EmptyViewAdapter();
        recyclerView = findViewById(R.id.rv_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(mAdapter);

        findViewById(R.id.btnReset).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRefresh();
            }
        });


    }

    @Override
    protected void onStart() {
        super.onStart();
        onRefresh();
    }

    private View getErrorView(){
        View errorView = getLayoutInflater().inflate(R.layout.error_view,recyclerView,false);
        errorView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRefresh();
            }
        });
        return errorView;
    }

    private View getNoDataView(){
        View noDataView = getLayoutInflater().inflate(R.layout.empty_view,recyclerView,false);
        noDataView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRefresh();
            }
        });
        return noDataView;
    }

    private void onRefresh() {
        mAdapter.setEmptyView(R.layout.loading_view);
        recyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (error) {
                    mAdapter.setEmptyView(getErrorView());
                    error = false;
                }else{
                    if (noData) {
                        mAdapter.setEmptyView(getNoDataView());
                        noData = false;
                    }else{
                        mAdapter.setList(DataServer.getSampleData(100));
                    }
                }
            }
        }, 1000);
    }
}
